package com.demo.cms.action.manage;

import java.util.List;

import com.demo.core.model.Site;
import com.demo.core.model.SiteFunction;
import com.demo.core.service.base.SiteFunctionService;
import com.demo.security.annotation.function.Function;
import com.demo.webapp.action.PageAction;
import com.demo.webapp.context.MultisiteContextHolder;

/**
 * 站点功能Action
 * 当前站点的功能操作
 * @author ryuu.kk
 *
 */
@Function(id = 101000, pid = 10, code = "manage.siteFunction")
public class SiteFunctionAction extends PageAction {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = -8332054592988418411L;
	
	/**
	 * 主键ID
	 */
	private Long id;
	
	/**
	 * 站点ID
	 */
	private Long siteId;
	
	/**
	 * 功能模型(树结构)
	 */
	private SiteFunction tree;

	/**
	 * 站点功能模型列表
	 */
	private List<SiteFunction> siteFunctionList;
	
	/**
	 * 站点功能模型
	 */
	private SiteFunction siteFunction;
	
	/**
	 * 站点功能模型服务
	 */
	private SiteFunctionService siteFunctionService;

	
	/**
	 * 功能模型
	 */
	@Function(id = 101001, pid = 101000, code = "manage.siteFunction.execute")
	public String execute() {
		
		// 取得站点模型
		Site site = MultisiteContextHolder.getSite();
		// 站点ID
		Long siteId = site.getId();
		
		if (id == null) {
			//id = Constants.FUNCTION_ROOT_ID;
			this.tree = siteFunctionService.queryFunctionTreeRoot(siteId);
		} else {
			this.tree = siteFunctionService.queryFunctionTree(this.id, siteId);
		}
		
		
		// 本站点分配的站点功能(List)
		this.siteFunctionList = siteFunctionService.query(siteId);
		
		return SUCCESS;
	}
	
	/**
	 * 功能树
	 * @return String
	 */
	@Function(id = 101070, pid = 101000, code = "manage.siteFunction.tree")
	public String tree() {
		// 取得站点ID
		Site site = MultisiteContextHolder.getSite();
		// 站点组ID
		Long siteId = site.getId();
		if (id == null) {
			//id = Constants.FUNCTION_ROOT_ID;
			// 站点最大功能集合(Tree)
			this.tree = siteFunctionService.queryFunctionTreeRoot(siteId);
		} else {
			// 站点最大功能集合(Tree)
			this.tree = siteFunctionService.queryFunctionTree(id, siteId);
		}
		return SUCCESS;
	}
	
	/**
	 * 创建站点功能信息
	 * @return String
	 */
	@Function(id = 101071, pid = 101000, code = "manage.siteFunction.enable")
	public String enable() {
		// 取得站点ID
		if (siteFunctionList != null) {
			for (SiteFunction sf : siteFunctionList) {
				siteFunctionService.update(sf.getId(), "RWD", sf.isEnabled());
			}
		}
		return CALLBACK;
	}
	
	/**
	 * 站点功能信息展示
	 * @return String
	 */
	@Function(id = 101072, pid = 101000, code = "manage.siteFunction.view")
	public String view() {
		// 取得站点ID
		Site site = MultisiteContextHolder.getSite();
		// 站点组ID
		Long siteId = site.getId();
		siteFunctionList = siteFunctionService.query(siteId, id);
		return SUCCESS;
	}
	
	//~~ settings here ---------------------------------------------------------------------
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public List<SiteFunction> getSiteFunctionList() {
		return siteFunctionList;
	}

	public void setSiteFunctionList(List<SiteFunction> siteFunctionList) {
		this.siteFunctionList = siteFunctionList;
	}

	public SiteFunction getSiteFunction() {
		return siteFunction;
	}

	public SiteFunction getTree() {
		return tree;
	}

	public void setSiteFunction(SiteFunction siteFunction) {
		this.siteFunction = siteFunction;
	}

	public Long getSiteId() {
		return siteId;
	}

	public void setSiteFunctionService(SiteFunctionService siteFunctionService) {
		this.siteFunctionService = siteFunctionService;
	}
}
